enum IOS_COMMAND {
  /** @desc 关闭浏览器指令 */
  CLOSE = 'onCloseBtnClick',
  /** @desc 实名认证 */
  AUTHENTICATE = 'gotoRealNameVCHandler',
  /** @desc 个人主页 */
  PERSON_HOME_PAGE = 'onUserCenterBtnClick',
  /** @desc 进入房间 */
  GO_TO_LIVE_ROOM = 'onGotoLiveRoom',
  /** @desc 资金明细 */
  FUNDING_DETAILS = 'gotoWalletDetailPageHandler',
  /** @desc 在线客服 */
  ONLINE_SERVICE = 'gotoMQLineServicePageHandler',
  /** @desc 调起移动端问题反馈页面 */
  FEED_BACK_HANDLE = 'feedBackHandler',
  /** @desc 注销账号回调 跳回登录页面 */
  CANCELLATION_ACCOUNT_SUCCESS_HANDLE = 'cancellationAccountSuccessHandler',
  /** @desc 客户端录音回调 */
  RECORD_HANDLE = 'recordHandle',
  /** @desc 跳转家族大厅 */
  FAMILY_HALL_HANDLE = 'familyHallHandle',
  /** @desc 跳转创建房间 */
  CREATE_STREAM_ROOM_HANDLE = 'createStreamRoomHandle',
  /** @desc 跳转游戏 */
  NEW_GAME = 'newGame',
  /** @desc 保存图片
   * picUrl:''
   * */
  SAVE_PIC = 'savePic',
  /**
   * @desc 分享页面
   * type:1-微信，2-朋友圈，3-QQ
   * url: ''
   * icon: ''
   * title: ''
   * text: ''
   *  */
  SHARE = 'share',
  /** @desc 分享图片到微信
   * picUrl:''
   * */
  SHARE_TO_WX = 'shareToWx',
  /** @desc 跳转修改资料 */
  UPDATE_INFO = 'updateInfo',
  /** @desc 跳转主播认证 */
  GOT_TO_PROCESS = 'gotoProcess',
  /** @desc 跳转充值页 */
  GO_TO_RECHARGE = 'gotoRecharge',
  /** @desc 跳转萌宠页 */
  GO_TO_PET = 'gotoPet',
  /** @desc 获取token */
  GET_TOKEN = 'getAppToken',
  /* 获取房间ID */
  GET_ROOMID = 'getRoomId',

}

/**
 * @desc 调起客户端方法参数
 * */
type Materials = {
  type: IOS_COMMAND;
  params: Record<string, any>;
  data: Record<string, any>;
};

const postMessage = (type: IOS_COMMAND, params: Record<string, any> = {}) => {
  try {
    let materials: Partial<Materials> = {type};
    let u = navigator.userAgent;
    let isiOS = !!u.match(/\(i[^;]+;( U;)? CPU.+Mac OS X/);
    let isAndroid = u.indexOf('Android') > -1 || u.indexOf('Adr') > -1;
    if (isiOS) {
      materials.params = params;
      window.webkit.messageHandlers.common.postMessage(
        JSON.stringify(materials),
      );
    } else if (isAndroid) {
      materials.data = params;
      window.bridge.postMessage(JSON.stringify(materials));
    }
  } catch (e) {
    console.error(`【${type}】 方法， 仅支持手机端回调`);
  }
};

export {postMessage, IOS_COMMAND};
